/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fyx.web;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import org.fyx.core.PermissionDeniedException;
import org.fyx.linker.web.LinkerBean;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.secure.entity.SecureUser;
import org.fyx.secure.ejb.AuthorizeFailException;
import org.fyx.secure.ejb.SecureLocal;
import org.fyx.web.context.WebContext;
import org.fyx.web.context.WebPage;
import org.primefaces.component.menuitem.MenuItem;

/**
 * Hlavní managed bean logování.
 * Zodpovídá za správné odeslálání požadavků na logování mezi klientem
 * a serverem.
 * 
 * @author xljan
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    @EJB
    private SecureLocal secure;
    @EJB
    private MulaLocal mula;
    @EJB
    private LinkerLocal linker;
    private String loginUrl = "";
    private SecureUser user = new SecureUser();

    public void LoginBean() {
    }

    public SecureUser getUser() {
        return user;
    }

    public void setUser(SecureUser user) {
        this.user = user;
    }

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

    /**
     * Vrati hodnotu pro atribut onClick v odkazu na logovani.
     * Pokud je uzivatel prihlasen vrati "" jinak skript
     * na otevreni prihlasovaciho dialogu.
     * 
     * @return 
     */
    public String getOnClick() {
        if (!WebContext.getCurrentContext().isLogedIn()) {
            return "dlgLogin.show()";
        }
        return "";
    }

    /**
     * Vrati hodnotu atrubutu value u odkazu na logovani.
     * Pokud je uzivatel prihlasen, vrati odkaz na stranku logout,
     * jinak javascript na moznost otevreni login dialogu.
     * 
     * @return 
     */
    public String getValue() {
        if (!WebContext.getCurrentContext().isLogedIn()) {
            return "javascript:void(0)";
        }
        return linker.getLink("secure/logout.xhtml");
    }

    /**
     * Vrati text odkazu na logovani.
     * Pokud je uzivatel prihlasen vrati Odhlasit,
     * jinak prihlasit.
     * 
     * @return 
     */
    public String getText() {
        if (!WebContext.getCurrentContext().isLogedIn()) {
            return mula.get("Login");
        }
        return mula.get("Logout");
    }

    /**
     * Provede autorizaci a autentizaci uzivatele.
     * Pokud nastane chyba, ohlasi uzivateli ze se jej nepodarilo
     * prihlasit.
     * 
     * @param actionEvent
     * @throws IOException 
     */
    public void login(ActionEvent actionEvent) throws IOException {
        try {
            secure.autentize(user);
            secure.authorize();
            linker.recreateMenuModel();
            WebContext.getMessages().add(new FacesMessage(
                    FacesMessage.SEVERITY_INFO,
                    mula.get("Welcome"), WebContext.getCurrentContext().
                    getUser().getUsername()));
            WebPage.redirect(linker.getLink(loginUrl));
        } catch (AuthorizeFailException ex) {
            WebPage.sendMessage(FacesMessage.SEVERITY_WARN,
                    mula.get("Login Error"), mula.get("Invalid credentials"));
        }
    }

    /**
     * Odhlasi uzivatele ze systemu.
     * 
     * @throws IOException 
     */
    public void logout() throws IOException {
        if (WebContext.getCurrentContext().isLogedIn()) {
            secure.logout();
            linker.recreateMenuModel();
            WebContext.getMessages().add(
                new FacesMessage(FacesMessage.SEVERITY_INFO, 
                    mula.get("Logout sucessfull"), ""));
            WebPage.redirect(WebContext.getCurrentContext().getBaseUrl());
        }
    }
}
